/**
 * @Description Ext.translate for ExtJS 2.x and 3.x; Auto translation with Google Translation API (java/php server side)
 * @author  Tomislav Milkovic
 * @license LGPLv3 http://www.opensource.org/licenses/lgpl-3.0.html
 * @version 1.5, 05.08.2009.
 */

/**
 * USAGE INFORMATION
 * 
 * For auto translation to work properly, following scripts must be included
 * in exact order as specified here and immediately after  ExtJs libs.
 * 
 * In Ext.on('ready'... function Ext.translate.Init function must be called 
 * before any other object creation. 
 *  
 * Ext.translate.js
 * Ext.translate.Manager.js
 * Ext.translate.Cache.js
 * Ext.translate.MessageBox.js
 * Ext.translate.Override.js
 */

/**
 * @todo html property to translate if required
 * @todo date translations
 * @todo implement update functions for all standard fields (only most important are done)
 * @todo Ext.grid.ColumnModel translation support   
**/

/**
 * @done client side caching to minimize server calls
 * @done client side caching store in cookies
 * @done cache indexing and speed optimization
 * @done translation before component is rendered with synchronous AJAX calls.
 * @done proxied AJAX support added
 * @done autoreset cookie if Ext.translate.version changed
 */

defined = function(test){return typeof test !== 'undefined';};

if (!navigator.cookieEnabled){
	 alert( 'WARNING: Browser cookies are disabled! \nPlease enable cookies to use all the features of automatic translation.\nIf cokies are disabled, component translation and application responsivity will be slower.');
};

Ext.namespace('Ext.translate');

//if set, proxied ajax calls will not remove scripttags
Ext.translate.debug = false;

/**
 * Used for tracking application version. (not version of this library) 
 * Cookies will be reset if version changed.
 * This is used to force reload new translation values no matter what.
 * If application is changed by adding new features, windows etc. 
 * This  option should be also changed in init script so translation engine 
 * resets cookies. This way browser will "synchronize" translations. 
 */
Ext.translate.version ='1.5';

Ext.translate.languages = [ [ 'ENGLISH', 'English', 'ux-flag-en' ] ];
Ext.translate.customLanguages = [];
Ext.translate.defaultLanguage = 'ENGLISH';
Ext.translate.currentLanguage = 'ENGLISH';
Ext.translate.oldLanguage = 'ENGLISH';

Ext.translate.Cookie = new Ext.state.CookieProvider();

//constants used for cookie names
Ext.translate.Cookie.language='exttran_lang';
Ext.translate.Cookie.version='exttran_ver';
Ext.translate.Cookie.prefix ='exttran_cache_';
/**
 * If true, calls are made directly to Google Translation Service
 */
Ext.translate.clientonly=false;

/**
 * URL of server side translation service
 *  Params 1
 *   @param languages - get list of supported languages
 *
 *  Params 2
 *   @param language - language to translate to
 *   @param translate - data to be translated
 *   
 * NOTE: Google API works best when input language is set as ENGLISH and 
 *  	 defaults should be in english. Server side as default uses english as
 *  	 language to be translated from.
 */

Ext.translate.URL = 'TranslateServlet';

/** 
 * URL where images of flags are. Used for dynamic generation of CSS for IconBox selector
 */
Ext.translate.languageImagesURL = 'img/flags/';
Ext.translate.autocreateIonsCSS = false;


/**
 *  Default ExtJS text fields showing on GUI components.
 *  If some are missing, they may be added freely but also function for updating
 *  gui must be implemented.
 *  Syntax for function name is 'get_'+fieldname
 *  
 *   @todo html requires special processing as tags must not be translated;
 *   idea is to find in DOM where html is rendered and then get innerText value to
 *   translate ?!
 */
Ext.translate.map_gui = [ 'fieldLabel', 'boxLabel', 'title', 'blankText',
		'invalidText', 'emptyText', 'maxLengthText', 'minLengthText',
		'loadingText', 'nanText', 'emailText', 'urlText', 'alphaText',
		'alphanumText', 'text', 'sortAscText', 'sortDescText', 'columnsText',
		'emptyGroupText', 'groupByText', 'showGroupsText', 'nameText',
		'valueText', 'splitTip', 'collapsibleSplitTip', 'disabledDaysText',
		'disabledDatesText', 'beforePageText', 'afterPageText', 'firstText',
		'prevText', 'nextText', 'lastText', 'refreshText', 'displayMsg',
		'emptyMsg', 'cancelText', 'monthYearText', 'todayText', 'ddText',
		'tooltip','header','html'];

// @todo - to implement default ExtJs text properties of non gui objects
Ext.translate.map_nogui = [ 'html' ];

// @todo add func for day and date text translation

Ext.translate.warned=false;
Ext.translate.isInitialized= function(){

	var  warnMe = function(){
			if (Ext.translate.isInitialized()!=true) {alert('WARNING: Translation engine is not initialized.\nTranslations are disabled!\nPlease use Ext.translate.Manager.Init function to initialize engine!');};		
	};
	
	try{
		return  Ext.translate.Manager.initialized==true;

	}catch(Ex){
		return false;
	}finally{
		if (Ext.translate.warned!=true){
			Ext.translate.warned=true;
			warnMe.defer(2000);
		}
	}
};

//copied from http://extjs.com/forum/showthread.php?t=12181&page=2
Ext.interceptConstructor = function(c, f) {
    var r = function(){
        f.apply(this, arguments);
        c.apply(this, arguments);
    };
    r.superclass = c.superclass;
    r.prototype = c.prototype;
    for (var i in c){
        r[i] = c[i];
    }
    return r;
};

